<template>
  <el-container class="app-wrapper">
    <el-aside width="200px" class="sidebar-container">
      <el-menu
        :default-active="$route.path"
        class="el-menu-vertical"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
        router
      >
        <el-menu-item index="/">
          <i class="el-icon-s-home"></i>
          <span slot="title">首页</span>
        </el-menu-item>

        <el-submenu index="/cabinets">
          <template slot="title">
            <i class="el-icon-box"></i>
            <span>暂存柜使用</span>
          </template>
          <el-menu-item index="/cabinets/A">所有柜子</el-menu-item>
        </el-submenu>

        <el-submenu index="/user">
          <template slot="title">
            <i class="el-icon-user"></i>
            <span>个人中心</span>
          </template>
          <el-menu-item index="/history">
            <span slot="title">使用记录</span>
          </el-menu-item>
        </el-submenu>

        <el-submenu index="/admin">
          <template slot="title">
            <i class="el-icon-setting"></i>
            <span>系统管理</span>
          </template>
          <el-menu-item index="/admin/cabinets">柜子管理</el-menu-item>
          <el-menu-item index="/admin/users">用户管理</el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header height="60px" class="header">
        <div class="header-left">
          <h2>暂存柜管理系统</h2>
        </div>
        <div class="header-right">
          <el-dropdown trigger="click" @command="handleCommand">
            <span class="el-dropdown-link">
              {{ username }}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>

      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState, mapGetters } from "vuex";

export default {
  name: "Layout",
  computed: {
    ...mapState({
      user: (state) => state.user
    }),
    ...mapGetters(["isAuthenticated"]),
    username() {
      return this.user ? this.user.username : "未登录";
    },
    isAdmin() {
      return this.user && this.user.role === "admin";
    }
  },
  methods: {
    handleCommand(command) {
      if (command === "logout") {
        this.$store.dispatch("logout");
        this.$router.push("/login");
      } else if (command === "profile") {
        this.$message.info("个人信息功能开发中");
      }
    }
  }
};
</script>

<style scoped>
.app-wrapper {
  height: 100vh;
}

.sidebar-container {
  background-color: #304156;
  height: 100%;
}

.el-menu {
  border-right: none;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-right {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}

.el-menu .el-submenu .el-menu-item {
  padding-left: 40px !important;
}
</style>
